package com.ghsc.ghxc.jsb.ui.my.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityMyTeamBinding
import com.ghsc.ghxc.jsb.ui.home.ListTypesFragment
import com.ghsc.ghxc.jsb.ui.home.bean.GoodBeanData
import com.ghsc.ghxc.jsb.ui.my.bean.TeamBeanData
import com.ghsc.ghxc.jsb.view.NotDataView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.my.activity
 * 类名：MyTeamActivity
 * 创建时间：2024/4/24 16:36
 * 创建人 @author： xiaolin
 * 描述 ：Todo...好有
 */
class MyTeamActivity  : BaseVMActivity<MyTeamVIewModel, ActivityMyTeamBinding>()  {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_my_team
    }

    override fun attachVMClass(): Class<MyTeamVIewModel>? {
        return MyTeamVIewModel::class.java
    }
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }



    private val mAdapter: ListAdapter by lazy { ListAdapter() }
    var mList = arrayListOf<TeamBeanData>()
    private var notDataView: View? = null

    override fun initView() {
        binding.apply {
            bar.topTitle.text = "我的好友"
            bar.topLeftView.setOnClickListener {
                finish()
            }
        }
        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)
        notDataView = NotDataView.notDataView(this, binding.rvList, binding.refreshLayout,"暂无数据")
        initRefreshLayout()
        pageNo = 1
        getListData()


    }
    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                pageNo = 1
                getListData()
            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }

    private fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["pindex"] = pageNo
        map["psize"] = pageSize
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onUserTeamAMethod(it1) }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@MyTeamActivity) {
                dismissLoading()
                binding.refreshLayout.finishRefresh()
                showToast(it)
            }
            againLoginMsg.observe(this@MyTeamActivity) {
                dismissLoading()
                binding.refreshLayout.finishRefresh()
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onUserTeamAData.observe(this@MyTeamActivity) {
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    it.data.data.zhibiao[0].forEach { (key, value) ->
                        binding.key1.text = key.toString()
                        binding.value1.text = value.toString()
                    }
                    it.data.data.zhibiao[1].forEach { (key, value) ->
                        binding.key2.text = key.toString()
                        binding.value2.text = value.toString()
                    }
                    it.data.data.zhibiao[2].forEach { (key, value) ->
                        binding.key3.text = key.toString()
                        binding.value3.text = value.toString()
                    }

                    if (it.data.list.isNullOrEmpty()) {
                        mAdapter.replaceData(it.data.list)
                        mAdapter.setEmptyView(notDataView!!)
                    }
                    else {
                        mAdapter.replaceData( it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }

            }
        }
    }



    inner  class ListAdapter  : BaseQuickAdapter<TeamBeanData, BaseViewHolder>(
        R.layout.my_team_list_item) {
        override fun convert(helper: BaseViewHolder, item: TeamBeanData) {
            helper.setText(R.id.mobile, item.mobile)
                .setText(R.id.crt_time, item.crt_time)
                .setText(R.id.recomcnt, item.recomcnt.toString())
                .setText(R.id.levelch, item.levelch)
                .setText(R.id.ifrenzheng,item.ifrenzheng)
        }
    }

}